﻿@using System.Runtime.Caching
@using FujiyBlog.Core.Caching
@using FujiyBlog.Core.EntityFramework
@using Newtonsoft.Json.Linq

@model FujiyBlog.Core.DomainObjects.WidgetSetting
@{
    Layout = "Widget.cshtml";

        JObject settings = JObject.Parse(Model.Settings??"{}");

    IEnumerable<Tuple<DateTime, int>> datesCount = CacheHelper.FromCacheOrExecute(() => DependencyResolver.Current.GetService<PostRepository>().GetArchiveCountByMonth(settings.Value<bool?>("RecentDatesAtTop").GetValueOrDefault()), cacheItemPolicy: new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(1) }, condition: !User.Identity.IsAuthenticated);
    var years = datesCount.Select(x => x.Item1.Year).Distinct();
}
<h4>@Html.Raw(settings.Value<string>("Title") ?? "Archive")</h4>
<div class="content">
    <ul class="monthlist">
        @foreach (var year in years)
        {
            <li class="year"><a href="javascript:void(0);" onclick="$(this).next().toggle('fast')">@year</a>
                <ul style="display: none">
                    @foreach (var mes in datesCount.Where(x => x.Item1.Year == year))
                    {
                        <li>@Html.ActionLink(System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.GetMonthName(mes.Item1.Month) + "(" + mes.Item2 + ")", MVC.Post.ArchiveDate(mes.Item1.Year, mes.Item1.Month, null))</li>
                    }
                </ul>
            </li>
        }
    </ul>
</div>
